import { Employee } from './../models/Employee';
import { LocalstorageDataManage } from './../common/localstoreage/localstoreageDataManage';
import { ExpeditionOrder } from './../models/expeditionOrder';
import { BehaviorSubject } from 'rxjs';
import * as R from "ramda";


export class EmployeeInfosManage {
    employeeInfos: Array<EmployeInfo>;
    observable: BehaviorSubject<Array<EmployeInfo>> = new BehaviorSubject([]);
    static current = new EmployeeInfosManage();

    constructor(employesInfos?: Array<EmployeInfo>) {
        this.init(employesInfos);
    }

    getObservable() {
        return this.observable;
    }

    notify() {
        this.observable.next(this.employeeInfos);
    }

    init(employesInfos?) {
        this.employeeInfos = employesInfos || this.loadFromCache();
        this.notify();
    }

    loadFromCache() { 
        return LocalstorageDataManage.current.getEmployeeInfos();
    }

    reset() {
        this.employeeInfos = this.employeeInfos.map(employeeInfo => {
            employeeInfo.hasDeliveryCount = 0;
            return employeeInfo;
        })
        this.notify();
    }


    saveEmployeInfosOnlineByIds(ids: Array<string>) {
        this.employeeInfos.filter(employeeInfo => ids.indexOf(employeeInfo.id) > -1).forEach(employeeInfo => employeeInfo.status = EmployeInfoStatus.online)
        this.employeeInfos.filter(employeeInfo => ids.indexOf(employeeInfo.id) == -1).forEach(employeeInfo => employeeInfo.status = EmployeInfoStatus.offline)
        this.notify();
        LocalstorageDataManage.current.setEmployeeInfos(this.employeeInfos);
    }

    saveEmployeInfosTurns(turn: string, employeeIds: Array<string>) {
        const employeeInfos = this.employeeInfos;
        employeeInfos.forEach(employeeInfo => {
            const turnIdx = employeeInfo.turns && employeeInfo.turns.indexOf(turn);
            if (turnIdx > -1) {
                employeeInfo.turns.splice(turnIdx, 1);
            }
        });
        employeeIds.forEach(id => {
            const employeeInfo = employeeInfos.find(x => x.id == id);
            employeeInfo.turns.unshift(turn);
            employeeInfo.turns = R.uniq(employeeInfo.turns);
        })
        this.notify();
        LocalstorageDataManage.current.setEmployeeInfos(this.employeeInfos);
    }

    getEntities() {
        if (!this.employeeInfos) this.init();
        return this.employeeInfos;
    }

    // 班次数组
    getTurns() {
        const turns = [];
        // turns.push('全部');
        this.employeeInfos.forEach(v => {
            turns.unshift(v.turns || []);
        })
        return R.uniq(R.flatten(turns));
    }

    getEmployeeInfosByTurn(turn: string): Array<EmployeInfo> {
        const turnedEmployees = this.employeeInfos.filter(info => info.turns && info.turns.indexOf(turn) > -1);
        return turnedEmployees;
    }

    getEmployeeInfosByTurns(turns: string[]): Array<EmployeInfo> {
        const turnedEmployees = this.employeeInfos.filter(info => R.intersection(info.turns || [], turns).length > 0);
        return turnedEmployees;
    }

    removeTurn(turn: string): Array<EmployeInfo> {
        const employeeInfos = this.employeeInfos;
        employeeInfos.forEach(employeeInfo => {
            employeeInfo.turns = employeeInfo.turns.filter(x => x != turn)
        })
        this.employeeInfos = employeeInfos;
        this.notify();
        LocalstorageDataManage.current.setEmployeeInfos(this.employeeInfos);
        return LocalstorageDataManage.current.getEmployeeInfos();
    }

    renameTurn(newTurn, oldTurn) {
        function replaceArray(array, find, replace) {
            return array.map(x => {
                if (x == find) x = replace;
                return x;
            })
        };

        const employeeInfos = this.employeeInfos;
        employeeInfos.forEach(employeeInfo => {
            employeeInfo.turns = replaceArray(employeeInfo.turns, oldTurn, newTurn);
        })
        this.employeeInfos = employeeInfos;
        this.notify();
        LocalstorageDataManage.current.setEmployeeInfos(this.employeeInfos);
        return LocalstorageDataManage.current.getEmployeeInfos();
    }

    updateEmployeeInfosByEmployees(employees: Array<Employee>) {
        const ids = employees.map(x => x.id);
        if (this.employeeInfos) {
            const currentIds = this.employeeInfos.map(x => x.id);
            const addIds = ids.filter(id => currentIds.indexOf(id) == -1);
            const addEmployeeInfos = addIds.map(x => {
                return {
                    id: x,
                    hasDeliveryCount: 0,
                    // status: EmployeInfoStatus.online,
                    status: EmployeInfoStatus.offline,
                    turns: ['全部']
                }
            });
            this.employeeInfos = this.employeeInfos.concat(addEmployeeInfos);
            this.employeeInfos = this.employeeInfos.filter(employeeInfo => {
                return ids.indexOf(employeeInfo.id) > -1;
            });
            LocalstorageDataManage.current.setEmployeeInfos(this.employeeInfos);
            return LocalstorageDataManage.current.getEmployeeInfos();
        }

    }
}


export interface EmployeInfo {
    id: string; //这里应该叫key? 是 entity 还是 value ，有ID的都是 entity么
    hasDeliveryCount: number;
    status: EmployeInfoStatus;
    turns: Array<string>
}

export enum EmployeInfoStatus {
    online,
    offline
}